package ru.urfc.portal.managers;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import ru.urfc.portal.dao.filter.Filter;
import ru.urfc.portal.model.accounting.Balance;
import ru.urfc.portal.model.accounting.Contract;
import ru.urfc.portal.model.accounting.Invoice;
import ru.urfc.portal.model.accounting.InvoiceFacture;
import ru.urfc.portal.model.accounting.UnifiedBalanceOperation;
import ru.urfc.portal.operations.BalanceOperationsAgregator;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AccountingManager extends BaseManager {
	
	@EJB BalanceOperationsAgregator balanceOperationsAgregator;

	public List<Contract> getContracts(Long clientId) {
		return getClientDescendants(Contract.class, clientId);
	}

	public Contract getContract(Long clientId, Long contractId) {
		return get(Contract.class, clientId, contractId);
	}
	
	public List<Invoice> getInvoices(Long clientId, Filter filter) {
		return getClientDescendants(Invoice.class, clientId, filter);
	}

	public Invoice getInvoice(Long clientId, Long invoiceId) {
		return get(Invoice.class, clientId, invoiceId);
	}

	public List<InvoiceFacture> getInvoiceFactures(Long clientId, Filter filter) {
		return getClientDescendants(InvoiceFacture.class, clientId, filter);
	}
	
	public List<UnifiedBalanceOperation> getBalanceOperations(Long clientId, Date fromDate, Date toDate) {
		return balanceOperationsAgregator.getBalanceOperations(getContragentId(clientId), fromDate, toDate);
	}

	public Balance getBalanceAt(Long clientId, Date date) {
		return balanceOperationsAgregator.calculateBalanceAt(getContragentId(clientId), date);
	}
	
}
